/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gimreader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Worker implements Runnable {

    static boolean debugmode = true;
    LogFiles filenode;
    String _hunter;
            
    static long utime(String s) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
        Date date;
        long longDate;
        try {
            date = sdf.parse(s);
            longDate = date.getTime();
        } catch (Exception e) {
            return -1;
        }
        return longDate;
    }

    static long findtime(String s, String filename) {
        Pattern p = Pattern.compile("(^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\,\\d{3})");
        Matcher m = p.matcher(s);
        if (m.find()) {
            return utime(m.group(1));
        } else {
            return 0;
        }
    }
    private String name;    //Thread Name
    Thread t;
    
    Worker(String threadname) {
        //logs = _logs;
        name = threadname;
        t = new Thread(this, name);
        System.out.println("Worker started: " + t);
        t.start();              // Thread start
    }

    // Thread Entry Point
    @Override
    public void run() {
        long ltime;            // used for time presentation in unix format
        int strings; 
        strings = 0;
        System.out.println("Processing Log file: " + name);

        //<editor-fold defaultstate="collapsed" desc="Parsing... ">
        try {
            BufferedReader logfile = new BufferedReader(new FileReader(name));
            String currentstr;
            try {
                while ((currentstr = logfile.readLine()) != null) {

                    strings++;
                                        
                    ltime = findtime(currentstr, name);
                    //System.out.println(ltime +" => "+currentstr);
                    if (ltime > 0) {
//                            Data.settimes(name, ltime);
                    }

                    if (currentstr.contains("Job 'Job_ExtractICON' started.")) {
                        if (debugmode) {
                            System.out.println("Extract started: \t" + currentstr.substring(0, 24)); //TODO remove this code
                        }
                        ltime = utime(currentstr.substring(0, 24));
                            //extract.SetStart(ltime);
                            //extract.SetVersion(currentstr.substring(89, 99));
                            //Data.setextractdata(ltime, new Extract(extract.GetStart(), extract.GetEnd(), extract.GetVersion()));
//                            extract.clear();

                    }


                    if (currentstr.contains("Job 'Job_ExtractICON' completed successfully.")) {
                        if (debugmode) {
                            System.out.println("Extract completed: \t" + currentstr.substring(0, 24)); //TODO remove this code
                        }
                        ltime = utime(currentstr.substring(0, 24));
                            //extract.SetEnd(ltime);
                        // TODO MUST BE Added to an Existing Extract if exists...

//                            Data.setextractdata(ltime, new Extract(extract.GetStart(), extract.GetEnd(), extract.GetVersion()));
//                            extract.clear();

                    }


                    if (currentstr.contains("Job 'Job_TransformGIM' started.")) {
                        if (debugmode) {
                            System.out.println("Transform started: \t" + currentstr.substring(0, 24)); //TODO remove debug code
                        }                            // Populate TransformStart Time
                        ltime = utime(currentstr.substring(0, 24));
                            //transform.SetStart(ltime);
                            //transform.SetVersion(currentstr.substring(90, 100));
//                            Data.settransformdata(ltime, new Transform(transform.GetStart(), transform.GetEnd(), transform.GetVersion()));
//                            transform.clear();
                    }



                    // Searching for Transformation delays due to not Extacted data
                    if (currentstr.contains("has been postponed due to delayed data")) {

                        //Populate delayed chunks info
                        if (debugmode) {
                            System.out.println("Transformation of chunk delayed " + currentstr.substring(currentstr.indexOf("Transformation of chunk") + 23, currentstr.indexOf("has been"))); //TODO remove this code
                        }
                    }



                    // Searching for Transform Completed Successfully
                    if (currentstr.contains("Job 'Job_TransformGIM' completed successfully.")) {
                        if (debugmode) {
                            System.out.println("Transform completed: \t" + currentstr.substring(0, 24)); // TODO remove this code
                        }
                        //populate Transform complete time
//                            transform.SetEnd(utime(currentstr.substring(0, 24)));

                        ltime = utime(currentstr.substring(0, 24));
//                            transform.SetEnd(ltime);
//                            Data.settransformdata(ltime, new Transform(transform.GetStart(), transform.GetEnd(), transform.GetVersion()));
//                            transform.clear();
                    }

                    // searhing for Failed Transforms
                    if (currentstr.contains("unsuccessfully")) {
                        if (debugmode) {
                            System.out.println("FAILED: " + currentstr.substring(0, 24));
                        }

                        // Populating failed transforms
                    }

                    // There should code which will parse the string and populate Job_Objects

                }
            } catch (IOException ex) {
                System.out.println("Unable to read from file " + logfile);
                return; //TODO remove this;
            }

            // Closing the file
            try {
                logfile.close();
            } catch (IOException ex) {
                System.out.println("Unable to close file " + logfile);
            }
            //</editor-fold>
        } catch (FileNotFoundException e) {
            System.out.print("file not found: " + name);
        }
    }
}
